import { inject } from "inversify";
import { controller, httpGet, httpPost } from "inversify-express-utils";
import { UserService } from "./service";
import { Request, Response } from "express";
import Result from "../../utils/Result";

@controller("/user")
export class UserController {
  constructor(@inject(UserService) private readonly server: UserService) {}

  /**
   * 添加用户
   * @param req 
   * @param res 
   */
  @httpPost("/add")
  public async addUser(req: Request, res: Response) {
    let { userName } = req.body;
    let result = await this.server.add(userName);
    res.send(Result.ok(result));
  }

  /**
   * 根据用户名获取用户
   */
  @httpGet("/getUser")
  public async getUser(req: Request, res: Response) {
    let { userName } = req.query;
    let result = await this.server.getUserByUserName(userName as string);
    res.send(Result.ok(result));
  }

  /**
   * 查询用户数量
   */
  @httpGet("/getUserCount")
  public async getUserCount(req: Request, res: Response) {
    let result = await this.server.getUserCount();
    res.send(Result.ok(result));
  }

}